﻿/**
* @author Alexander Rindo
* @project Flashview
* @version 0.4
* @function Document Class
**/

package com.alexrindo.flashview 
{
	import com.alexrindo.flashview.contextmenu.ImageContextMenu;
	import com.alexrindo.flashview.data.GalleryData;
	import com.alexrindo.flashview.image.ImageManager;
	import com.alexrindo.flashview.layout.LayoutManager;
	import com.alexrindo.flashview.listeners.KeyListener;
	import com.alexrindo.flashview.UI.ButtonManager;
	import com.alexrindo.flashview.UI.fullscreen.FullScreenToolBar;
	import com.alexrindo.flashview.UI.thumbnails.ThumbnailHolder;
	import com.alexrindo.flashview.UI.zoomSlider;
	import com.alexrindo.flashview.xml.XMLParser;
	import com.pixelbreakerAS3.ui.osx.MacMouseWheel;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageDisplayState;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	
	public class Flashview extends Sprite
	{
		private static const XML_URL:String = "data.xml"
		
		private var _parser:XMLParser
		private var _data:GalleryData
		private var _layout:LayoutManager
		private var _images:ImageManager
		private var _buttons:ButtonManager
		private var _keyListener:KeyListener
		private var _zoomSlider:zoomSlider
		private var _captionText:TextField
		private var _captionFormat:TextFormat
		private var _captionBar:captionBar
		private var _fullScreenToolBar:FullScreenToolBar
		private var _oldStageDisplayState:String
		private var _coverLayer:Sprite
		private var _dragHolder:Sprite
		
		public function Flashview() 
		{
			createLayout()
			buildUserInterface()
			loadXML()
		}
		
		private function createLayout():void
		{
			stage.scaleMode = StageScaleMode.NO_SCALE
			stage.align = StageAlign.TOP_LEFT
			
			_layout = LayoutManager.getInstance(stage)
			_layout.addStageObject(rightToolbar,{xDistance:-30,yDistance:-30},LayoutManager.RIGHT_CORNER)
			_layout.addStageObject(leftToolbar, { xDistance:30, yDistance: -30 }, LayoutManager.LEFT_CORNER)
			_layout.imageOffset = 55
			
			_oldStageDisplayState = StageDisplayState.NORMAL
			stage.addEventListener(Event.RESIZE, onStageResize)
			thumbHolder.scrollbar.resizeElements()
			
			MacMouseWheel.setup(this.stage)
		}
		
		private function loadXML():void
		{
			_parser = new XMLParser()
			_parser.addEventListener(_parser.PARSE_COMPLETE, onXMLParseComplete)
			_parser.processXMLData(XML_URL)
			
			_data = GalleryData.getInstance()
			_images = ImageManager.getInstance()
			_images.slider = _zoomSlider
		}
		
		private function onXMLParseComplete(e:Event):void
		{
			setupImages()
			addStageListeners()
			setContextMenu()
			_images.addListeners()
			thumbHolder.addThumbnails()
		}
		
		private function setupImages():void
		{
			ImageContainer.addChild(_images)
			setImageCount()
		}
		
		private function buildUserInterface():void
		{
			_fullScreenToolBar = new FullScreenToolBar()
			_fullScreenToolBar.visible = false
			addChild(_fullScreenToolBar)
			
			_buttons = ButtonManager.getInstance()
			_buttons.addButton(rightToolbar.nextImage, ButtonManager.NEXT_ARROW)
			_buttons.addButton(rightToolbar.prevImage, ButtonManager.PREV_ARROW)
			_buttons.addButton(leftToolbar.saveIcon, ButtonManager.SAVE_BUTTON)
			_buttons.addButton(leftToolbar.shareIcon, ButtonManager.SHARE_BUTTON)
			_buttons.addButton(leftToolbar.infoIcon, ButtonManager.INFO_BUTTON)
			_buttons.addButton(leftToolbar.fullscreenIcon, ButtonManager.FULLSCREEN_BUTTON)
			_buttons.addButton(_fullScreenToolBar.nextImage, ButtonManager.FULLSCREEN_NEXT_ARROW)
			_buttons.addButton(_fullScreenToolBar.prevImage, ButtonManager.FULLSCREEN_PREV_ARROW)
			_buttons.addButton(_fullScreenToolBar.playButton, ButtonManager.PLAY_PAUSE_BUTTON)
			_buttons.addButton(_fullScreenToolBar.exitButton, ButtonManager.EXIT_BUTTON)
			_buttons.addListeners()
			_buttons.setStageObject(stage)
			
			_zoomSlider = new zoomSlider(stage)
			_layout.addStageObject(_zoomSlider, { xDistance: -139, yDistance: 0 }, LayoutManager.CENTER_RIGHT)
			_zoomSlider.alpha = 0
			_zoomSlider.x = 950
			_zoomSlider.y = 100
			addChild(_zoomSlider)
		}
		
		private function setContextMenu():void
		{
			if (_data.protectImages == false)
			{
			this.contextMenu = new ImageContextMenu().getContextMenu()
			}
		}
		
		private function addStageListeners():void
		{
			_images.addEventListener(ImageManager.IMAGE_INIT, onImageInit)
			_images.addEventListener(ImageManager.IMAGE_CHANGE, onImageChange)
			_images.addEventListener(ImageManager.IMAGE_ZOOM, positionCaptionBar)
			_images.addEventListener(ImageManager.IMAGE_MOVE, positionCaptionBar)
			_images.addEventListener(ImageManager.START_DRAG, moveToFront)
			_images.addEventListener(ImageManager.STOP_DRAG, moveToBack)
			thumbHolder.addEventListener(ThumbnailHolder.POSITION_CHANGE, positionCaptionBar)
			_keyListener = new KeyListener(stage)
		}
		
		private function moveToFront(e:Event)
		{
			if (_coverLayer == null)
			{
				_coverLayer = new Sprite()
				_dragHolder = new Sprite()
				addChild(_coverLayer)
				addChild(_dragHolder)
			} else {
				_coverLayer.visible = true
			}
			
			_dragHolder.addChild(_images)
			
			_coverLayer.graphics.clear()
			_coverLayer.graphics.beginFill(0x111111, 0.3)
			_coverLayer.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight)
			_coverLayer.graphics.endFill()
		}
		
		private function moveToBack(e:Event)
		{
			_coverLayer.graphics.clear()
			_coverLayer.visible = false
			ImageContainer.addChild(_images)
		}
		
		private function onImageInit(e:Event):void
		{
			setImageCount()
		}
		
		private function onImageChange(e:Event):void
		{
			setCaptionBar()
			
			if (_images.isCurrentImageLoaded)
			{
				positionCaptionBar()
			} 
		}
		
		private function setImageCount():void
		{
			rightToolbar.ImageCount.text = "Image " + (_images.index+1) + " / " + _data.pictureCount
		}
		
		private function setCaptionBar():void
		{
			if (_captionBar == null)
			{
				_captionBar = new captionBar()
				addChild(_captionBar)
			}
			_captionBar.captionText.text = _data.getCaption(_images.index)
			_captionBar.middle.width = _captionBar.captionText.textWidth - 15
			_captionBar.top.x = _captionBar.middle.width + 20
			_captionBar.captionText.width = _captionBar.captionText.textWidth + 10
			_captionBar.x = stage.stageWidth / 2 - _captionBar.width / 2 
			_captionBar.visible = false
		}
		
		private function positionCaptionBar(e:Event = null):void
		{
			if (_captionBar != null)
			{
				_captionBar.x = _images.getCurrentImagePosition().x + (_images.getCurrentImageWidth()/2) - (_captionBar.width / 2)
				_captionBar.y = _images.getCurrentImagePosition().y + _images.getCurrentImageHeight()  + 40
				if (stage.displayState != StageDisplayState.FULL_SCREEN)
				{
				_captionBar.visible = true
				}
			}
		}
		
		private function onStageResize(e:Event):void 
		{
			_layout.setLayout()
			thumbHolder.scrollbar.resizeElements()
			
			if (_images.isCurrentImageLoaded == true)
			{
				_images.drawFrame()
				positionCaptionBar()
			}

			if (stage.displayState != _oldStageDisplayState)
			{
			checkForFullScreenToggle()
			_oldStageDisplayState = stage.displayState
			_images.drawFrame()
			positionCaptionBar()
			}
		}
		
		private function checkForFullScreenToggle():void
		{
			switch (stage.displayState)
			{
				case StageDisplayState.NORMAL:
					showNormalInterface()
					enableListeners()
					_images.stopSlideShow()
					break
					
				case StageDisplayState.FULL_SCREEN:
					showFullScreenInterface()
					positionFullScreenToolbar()
					disableListeners()
					_images.startSlideShow()
					
					break
			}
			
			_layout.setLayout()
		}
		
		private function showNormalInterface():void
		{
			leftToolbar.visible = true
			rightToolbar.visible = true
			thumbHolder.visible = true
			
			_captionBar.visible = true
			_zoomSlider.visible = true
			_fullScreenToolBar.visible = false
			
			_layout.imageOffset = 55
		}
		
		private function showFullScreenInterface():void
		{
			leftToolbar.visible = false
			rightToolbar.visible = false
			thumbHolder.visible = false
			
			_captionBar.visible = false
			_zoomSlider.visible = false
			_fullScreenToolBar.visible = true
			
			_layout.imageOffset = 0
		}
		
		private function enableListeners():void
		{
			_images.addEventListener(ImageManager.START_DRAG, moveToFront)
			_images.addEventListener(ImageManager.STOP_DRAG, moveToBack)
		}
		
		private function disableListeners():void
		{
			_images.removeEventListener(ImageManager.START_DRAG, moveToFront)
			_images.removeEventListener(ImageManager.STOP_DRAG, moveToBack)
		}
		
		private function positionFullScreenToolbar():void
		{
			_fullScreenToolBar.x = _images.getCurrentImagePosition().x + (_images.getCurrentImageWidth()/2) - (_fullScreenToolBar.width / 2)
			_fullScreenToolBar.y = _images.getCurrentImagePosition().y + _images.getCurrentImageHeight()  + 40
		}
	}
}
